#ifndef PRINTER_H
#define PRINTER_H

#include <QObject>
#include <QJsonObject>
#include <QPainter>
#include <QImage>
#include "record.h"
#include "printerclient.h"


class PrinterComponent : public QObject {
    Q_OBJECT
public:
    explicit PrinterComponent(QObject *parent = nullptr):QObject(parent){};

    static PrinterComponent* FromJson(QJsonObject &obj);

    void Draw(QPainter &painter, Record &r);

private:
    QString getText(Record &r);

private:
    int x;
    int y;
    int h;
    int w;
    QString name;
    int fontSize;
    bool fontWeight;
    QString textAlign;
    QString value;

};

class Printer : public QObject
{
    Q_OBJECT
public:
    explicit Printer(QObject *parent = nullptr);

    void SetPrinterInfo(
                    int PrinterDpi,
                    QString PrinterName,
                    QString PrinterIp,
                    QString PrinterPort,
                    int HShift,
                    int VShift,
                    QString PrinterXML,
        QString PrinterTemplateVersion);

    void PrintRecord(Record& record);

private:
    QImage drawImage(Record& record);
    QByteArray getBitArray(QImage &grayImage);
    void sendToPrinter(QByteArray &byte);
    PrinterClient* createPrinter(QString ip, int port);

private:
    int dpi;
    QString name;
    QString ip;
    int port;
    int hShift;
    int vShift;
    QString templateVersion;
    PrinterClient* client;


    int width;
    int height;
    QVector<PrinterComponent*> components;

    bool isSuccess;

signals:
    void printerFailed(const QString &error);
};

#endif // PRINTER_H
